package com.yuexun.beilunpatient.ui.registration.ui;

import android.os.CountDownTimer;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.base.BaseKJActivity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentAuthPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentAuthView;

import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chenss on 2018/11/13.
 */
public class Act_Appointment_Auth extends BaseKJActivity implements IAppointmentAuthView {

    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.auth_code)
    EditText authCode;
    @Bind(R.id.authBt)
    TextView authBt;

    AppointmentAuthPresenter presenter;

    String phone,password,loginName;

    @Override
    public void setRootView() {
        setContentView(R.layout.act_appointment_auth);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        presenter = new AppointmentAuthPresenter(new RegistrationModel(), this);
        tvPhone.setText(getIntent().getExtras().getString("phone"));
        phone = getIntent().getExtras().getString("phone");
        password = getIntent().getExtras().getString("password");
        loginName = getIntent().getExtras().getString("loginName");
        timeing();
    }



    @OnClick({R.id.title_back, R.id.authBt, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.authBt:
                if (!tvPhone.getText().toString().isEmpty() && tvPhone.getText().toString().trim().length() == 11) {
                    Map<String, String> params = new HashMap<>();
                    params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                    params.put("ptlyfd", "5");
                    params.put("phone", phone);
                    params.put("password", password);
                    params.put("loginName", loginName);
                    presenter.sendActiveCodeNoPassWord(params);
                    timeing();
                } else {
                    ViewInject.toast("手机号不能为空");
                }
                break;
            case R.id.next:
                if(!authCode.getText().toString().isEmpty()){
                    Map<String, String> params = new HashMap<>();
                    params.put("sessionUuidPatient", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
                    params.put("verCode", authCode.getText().toString());
                    params.put("loginName", loginName);
                    params.put("loginPassword", password);
                    presenter.appointmentActivationNoPassword(params);
                }else{
                    ViewInject.toast("请输入验证码");
                }
                break;
        }
    }

    private void timeing()
    {
        /** 倒计时60秒，一次1秒 */
        CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                // TODO Auto-generated method stub
                authBt.setText("还剩" + millisUntilFinished / 1000 + "秒");
                authBt.setEnabled(false);
            }

            @Override
            public void onFinish() {
                authBt.setText("获取验证码");
                authBt.setEnabled(true);
            }
        }.start();
    }

    @Override
    public void sendActiveCodeNoPassWord(BaseEntity<String> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            ViewInject.toast("短信验证码已发送尾号" + tvPhone.getText().toString().substring(7) + "手机!");
        }
        else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }

    @Override
    public void appointmentActivationNoPassword(BaseEntity<String> baseEntity) {
        if(baseEntity.getStatus().equals("200")){
            ViewInject.toast("激活成功");
            finish();
        }
        else {
            ViewInject.toast(baseEntity.getMessage());
        }
    }
}
